let timerInterval = null;
let remainingTime = 25 * 60; // 默认25分钟
let timerDuration = 25 * 60; // 保存当前设置的时长

function initTimer() {
    const display = document.getElementById('timer-display');
    const startBtn = document.getElementById('timer-start');
    const pauseBtn = document.getElementById('timer-pause');
    const resetBtn = document.getElementById('timer-reset');
    const durationSelect = document.getElementById('timer-duration');

    // 初始化时长选择器
    if (!durationSelect) {
        const timerContainer = document.querySelector('.timer-container');
        const selectContainer = document.createElement('div');
        selectContainer.className = 'timer-duration';
        selectContainer.innerHTML = `
            <label>时长选择：</label>
            <select id="timer-duration">
                <option value="5">5分钟</option>
                <option value="10">10分钟</option>
                <option value="15">15分钟</option>
                <option value="25" selected>25分钟</option>
                <option value="30">30分钟</option>
                <option value="45">45分钟</option>
                <option value="60">60分钟</option>
            </select>
        `;
        timerContainer.insertBefore(selectContainer, display);
    }

    // 获取时长选择器并添加事件监听
    const durationSelectEl = document.getElementById('timer-duration');
    durationSelectEl.onchange = (e) => {
        const minutes = parseInt(e.target.value);
        timerDuration = minutes * 60;
        resetTimer(display);
    };

    startBtn.onclick = () => startTimer(display);
    pauseBtn.onclick = pauseTimer;
    resetBtn.onclick = () => resetTimer(display);

    updateTimerDisplay(display, remainingTime);
}

function cleanupTimer() {
    pauseTimer();
    const startBtn = document.getElementById('timer-start');
    const pauseBtn = document.getElementById('timer-pause');
    const resetBtn = document.getElementById('timer-reset');
    const durationSelect = document.getElementById('timer-duration');
    
    startBtn.onclick = null;
    pauseBtn.onclick = null;
    resetBtn.onclick = null;
    if (durationSelect) durationSelect.onchange = null;
}

// 重置计时器使用当前选择的时长
function resetTimer(display) {
    pauseTimer();
    remainingTime = timerDuration;
    updateTimerDisplay(display, remainingTime);
}

// 其他函数保持不变...
function startTimer(display) {
    if (timerInterval) return;
    timerInterval = setInterval(() => {
        if (remainingTime > 0) {
            remainingTime--;
            updateTimerDisplay(display, remainingTime);
        } else {
            clearInterval(timerInterval);
            timerInterval = null;
            alert('番茄钟结束！休息一下吧～');
        }
    }, 1000);
}

function pauseTimer() {
    clearInterval(timerInterval);
    timerInterval = null;
}

function updateTimerDisplay(display, seconds) {
    const minutes = Math.floor(seconds / 60);
    const secs = seconds % 60;
    display.textContent = `${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}